package com.ld.security.common.mybatis.core.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ld.security.common.core.constant.CSuperConstant;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.core.util.ReflectionUtil;
import com.ld.security.common.core.util.SimpDateUtil;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.mybatis.anno.SqlKey;
import com.ld.security.common.mybatis.base.constant.SuperConstant;
import com.ld.security.common.mybatis.constant.SqlCondition;
import com.ld.security.common.mybatis.util.SqlTool;
import com.ld.security.common.mybatis.util.TypeUtil;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

public class AbstWrapper<E> extends QueryWrapper<E> {


    //分组后的字段
    private List<SqlTool.fieldVo> fieldList;

    private static void initData(QueryWrapper qw ,Object args, SqlTool.fieldVo fieldVo){
        for (Field field : fieldVo.getFields()) {
            SqlKey sqlKey =    field.getAnnotation(SqlKey.class);
            if(sqlKey == null){
                continue;
            }
            Object val =  ReflectionUtil.invokeGetter(args,field.getName());
            if(ReflectionUtil.isNull(val)){
                continue;
            }
            String name =null;
            if(sqlKey != null){
                name = sqlKey.name();
            }
            if(StringUtil.isNull(name)) {
                name = field.getName();
            }
            name = StringUtil.javaToColumn(name);
            if(!sqlKey.and()){
                qw.or();
            }
            addColumn(qw,name,sqlKey.value(),val);

        }
    }

    private static <E> void addColumn(
            QueryWrapper<E> qw, String columnName, SqlCondition condition, Object val) {

        // 值预处理
        if (val == null) {
            return;
        } else if (val instanceof String) {
            if (StringUtil.isNull((String) val)) {
                return;
            }
        } else if (val instanceof CSuperConstant.EnSuperEnum) {
            SuperConstant.SuperEnum se = (SuperConstant.SuperEnum) val;
            val = se.getValue();
        } else if (val instanceof Collection) {
            Collection col = (Collection) val;
            if (col.isEmpty()) {
                return;
            }
        }

        //日期处理
        if(val instanceof  List){
            List<?> list = (List) val;
            if(list.get(0) instanceof  Date){
                val =  list.stream().map(v -> {
                    Date d = (Date) v;
                    return SimpDateUtil.fomat(d);

                }).collect(Collectors.toList());
            }


        }else  if(val instanceof Date){
            val =  SimpDateUtil.fomat((Date)val);
        }

        //怀疑这个是日期
        if(condition== SqlCondition.between){
            if(val instanceof String){
                List vs = TypeUtil.getDateList(val.toString());
                if(vs != null){
                    val = vs;
                }
            }
        }



        switch (condition) {
            case eq:
            {
                qw.eq(columnName, val);
                break;
            }
            case ne:
            {
                qw.ne(columnName, val);
                break;
            }
            case gt:
            {
                qw.gt(columnName, val);
                break;
            }
            case ge:
            {
                qw.ge(columnName, val);
                break;
            }
            case lt:
            {
                qw.lt(columnName, val);
                break;
            }
            case le:
            {
                qw.le(columnName, val);
                break;
            }
            case like:
            {
                qw.like(columnName, val.toString());
                break;
            }
            case like_left:
            {
                qw.likeLeft(columnName, val.toString());
                break;
            }
            case like_right:
            {
                qw.likeRight(columnName, val.toString());
                break;
            }
            case like_many:
            {
                Collection collection = (Collection) val;
                for (Object v : collection) {
                    qw.or();
                    qw.like(columnName, v);
                }
                break;
            }
            case in:
            {
                qw.in(columnName, (Collection<?>) val);

                break;
            }
            case between:
            {
                // 区间查询
                if (val instanceof String) {
                    String str = val.toString();
                    String[] keys = str.split("-");
                    qw.between(columnName, keys[0], keys[1]);
                } else if (val instanceof List) {
                    List keys = (List) val;
                    qw.between(columnName, keys.get(0), keys.get(1));
                } else {
                    ApiException.throw400("异常语法");
                }

                break;
            }
        }
    }

     public AbstWrapper addField(String fieldName,SqlCondition sqlCondition,Object val){
        String name = StringUtil.javaToColumn(fieldName);
         addColumn(this,name,sqlCondition,val);
        return this;
     }


    protected void init(Object arg){

        fieldList = SqlTool.groupField(arg.getClass());
            for (SqlTool.fieldVo fieldVo : fieldList) {
                if(fieldList.size() == 1){
                        initData(this,arg,fieldVo);
                }else{
                    if(fieldVo.hasValue(arg)){
                        this.and(( p) -> {
                            initData(p,arg,fieldVo);
                            return p;
                        });
                    }

                }


            }


    }


}
